package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Grade;
import cn.wolfcode.crm.domain.Spending;
import cn.wolfcode.crm.query.SpendingQueryObject;
import cn.wolfcode.crm.service.ISpendingService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("spending")
public class SpendingController {
    @Autowired
    private ISpendingService spendingService;

    @RequestMapping("view")
    public String query() {
        return "spending";
    }


    @RequestMapping("list")
    @ResponseBody
    public PageResult list(SpendingQueryObject qo){
        return spendingService.getAll(qo);
    }

    @RequestMapping("saveOrUpdate")
    public JSONResult saveOrUpdate(Spending spending){
        JSONResult jsonResult = new JSONResult();
        spendingService.saveOrUpdate(spending);
        return jsonResult;
    }


    @RequestMapping("audit")
    @ResponseBody
    public JSONResult audit(long id){
        JSONResult jsonResult = new JSONResult();
        try {
            spendingService.audit(id);
            return jsonResult;
        }catch (Exception e){
            jsonResult.remark(e.getMessage());
            return jsonResult;
        }
    }
    @RequestMapping("theAudit")
    @ResponseBody
    public JSONResult theAudit(long id){
        JSONResult jsonResult = new JSONResult();
        spendingService.theAudit(id);
        return jsonResult;
    }


    @RequestMapping("rem")
    @ResponseBody
    public JSONResult rem(long id){
        JSONResult jsonResult = new JSONResult();
        try {
            spendingService.delete(id);
            return jsonResult;
        }catch (Exception e){
            jsonResult.remark(e.getMessage());
            return jsonResult;
        }
    }


    @RequestMapping("export")
    public void export(HttpServletResponse resp) throws IOException {
        //设置下载响应头
        resp.setHeader("Content-Disposition","attachment;filename=spending.xls");
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建工作簿
        HSSFSheet sheet = wb.createSheet();
        //查询支出数据
        List<Spending> spendings = spendingService.All();
        //创建第一行
        HSSFRow row = sheet.createRow(0);
        //创建单元格,设置标题
        row.createCell(0).setCellValue("支出时间");
        row.createCell(1).setCellValue("支出金额");
        row.createCell(2).setCellValue("备注");
        row.createCell(3).setCellValue("支出方式");
        row.createCell(4).setCellValue("花费类型");
        row.createCell(5).setCellValue("单号");
        row.createCell(6).setCellValue("审核状态");
        row.createCell(7).setCellValue("支出人员");
        row.createCell(8).setCellValue("经手人员");
        row.createCell(9).setCellValue("审核人员");
        row.createCell(10).setCellValue("支出班级");
        for (int i = 0;i < spendings.size(); i++){
            //从第二行开始再创建行
            row = sheet.createRow(i + 1);
            Spending spending = spendings.get(i);
            //创建单元格,填充内容
            Date time = spending.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(time);
            row.createCell(0).setCellValue(date);
            BigDecimal paymoney = spending.getPaymoney();
            row.createCell(1).setCellValue(paymoney.toPlainString());
            row.createCell(2).setCellValue(spending.getRemark());
            row.createCell(3).setCellValue(spending.getPaytype());
            row.createCell(4).setCellValue(spending.getConsumptiontype());
            row.createCell(5).setCellValue(spending.getSn());
            if (spending.isState()) {
                row.createCell(6).setCellValue("已审核");
            }else {
                row.createCell(6).setCellValue("未审核");
            }
            Employee payMan = spending.getPayMan();
            if (payMan != null) {
                row.createCell(7).setCellValue(payMan.getUsername());
            }
            Employee handleMan = spending.getHandleMan();
            if (handleMan != null) {
                row.createCell(8).setCellValue(handleMan.getUsername());
            }
            Employee auditMan = spending.getAuditMan();
            if (handleMan != null) {
                row.createCell(9).setCellValue(auditMan.getUsername());
            }
            Grade grade = spending.getGrade();
            if (grade != null) {
                row.createCell(10).setCellValue(grade.getClassName());
            }

        }
        //输出到浏览器
        wb.write(resp.getOutputStream());
    }
}
